/* eslint-disable no-unused-vars */
import { constantRoutes } from '@/router';
import Vue from 'vue';
import Router from 'vue-router';
import { getMenuList } from '@/api/login';
import Layout from '@/layout';
import { Message } from 'element-ui';
import { asyncAdminRoutes } from '@/router';
import { getToken } from '@/utils/auth.js';
Vue.use(Router);

function filterAsyncRouter(asyncRouterMap) {
  const accessedRouters = asyncRouterMap.filter(route => {
    route.meta = {
      title: route.title,
      icon: route.icon
    };
    if (route.component) {
      if (route.component === 'Layout') {
        route.component = Layout;
      } else {
        const component = route.component;
        route.componentPath = component;
        route.component = resolve => require(['@/views' + component], resolve);
      }
    }
    if (route.children && route.children.length > 0) {
      route.alwaysShow = true;
      route.children = filterAsyncRouter(route.children);
    }
    return true;
  });
  return accessedRouters;
}
const state = {
  routes: [],
  addRoutes: [],
  roleName: '',
  defaultPath: '/xmReport',
  menuList: []
};
const mutations = {
  setPath: (state, newVal) => {
    state.defaultPath = newVal;
  },
  setRoleName: (state, name) => {
    state.roleName = name;
  },
  SET_ROUTES: (state, routes) => {
    state.addRoutes = routes;
    state.routes = constantRoutes.concat(routes);
  },

  specialChangeRoutes: (state, result) => {
    setTimeout(() => {
      state.routes.map(o => {
        if (!o.title) {
          o.hidden = true;
        } else {
          if (o.title.indexOf('全景图') !== -1) {
            o.hidden = false;
            o.children.map(o => {
              o.hidden = false;
              if (o.title === result) {
                o.hidden = false;
              } else {
                o.hidden = true;
              }
            });
          } else {
            o.hidden = true;
          }
        }
      });
    }, 200);
  },
  changeRoutes: (state, result) => {
    const allButton = state.routes.filter(item => item.path.indexOf(result) !== -1);
    if (allButton.length !== state.routes.length && allButton.length !== 0) {
      if (allButton[0].children) {
        allButton[0].children.map(o => {
          if (allButton[0].redirect === o.path) {
            localStorage.setItem('buttonType', JSON.stringify(o.auths));
          }
        });
      }
    }
    const isShowAllRoute = localStorage.getItem('isShowAllRoute');
    const cacheCqc = localStorage.getItem('cache');
    if (!isShowAllRoute === '1' || cacheCqc !== 'cqc') {
      state.routes.map(o => {
        if (o.path.indexOf(result) !== -1) {
          o.hidden = false;
        } else {
          o.hidden = true;
        }
      });
    }
  },
  SET_MENUELIST: (state, data) => {
    state.menuList = data;
  }
};
const actions = {
  generateRoutes({ commit }, roles) {
    return new Promise(resolve => {
      let accessedRoutes;
      if (roles === '06') {
        accessedRoutes = asyncAdminRoutes || [];
        commit('SET_ROUTES', accessedRoutes);
        resolve(accessedRoutes);
      } else {
        getMenuList({
          roleId: getToken('xcRoleId'),
          state: 4,
          url: '',
          appId: getToken('sysitemId'),
          isMeta: true
        }).then(response => {
          if (response.success) {
            const a = response.data;
            commit('SET_MENUELIST', a);
            accessedRoutes = filterAsyncRouter(a);
            commit('SET_ROUTES', accessedRoutes);
            resolve(accessedRoutes);
          } else {
            Message({
              message: '没有访问权限',
              type: 'warning'
            });
            setTimeout(() => {
              window.localStorage.clear();
              window.location.reload();
            }, 1000);
          }
        });
      }
    });
  },
  getRoleName() {
    return new Promise(resolve => {
      resolve(state.roleName);
    });
  }
};
export default {
  namespaced: true,
  state,
  mutations,
  actions
};
